#import "WorkitemEntry.h"
@implementation WorkitemEntry

@synthesize workitemId;
@synthesize processInstanceId;
@synthesize taskName;
@synthesize processTitle;
@synthesize processModelName;
@synthesize offeredDate;

- (id)initWithJsonDic:(NSDictionary*)dictionary {
	self = [super init];
	if(self != nil) {
        NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
		workitemId = [(NSDecimalNumber*)[dictionary objectForKey:@"id"] longValue];
		processInstanceId = [(NSDecimalNumber*)[dictionary objectForKey:@"processInstanceId"] longValue];
		taskName = [[NSString alloc] initWithString:[dictionary objectForKey: @"nodeName"]];
		processModelName = [[NSString alloc] initWithString:[dictionary objectForKey: @"processModelInfoName"]];
		if([dictionary objectForKey: @"processInstanceTitle"] == [NSNull null]) {
			processTitle = [[NSString alloc] initWithString: @"(件名なし)"];
		} else {
			processTitle = [[NSString alloc] initWithString: [dictionary objectForKey: @"processInstanceTitle"]];
		}
        offeredDate = [dateFormatter dateFromString:[dictionary objectForKey:@"offerDatetime"]];
        [offeredDate retain];
	}
	/*
    NSLog(@"WorkitemEntry workitemId:[%d] taskName:[%@] processModelName:[%@] processTitle:[%@] offeredDate:[%@] processInstanceId:[%d]"
          , workitemId, taskName, processModelName, processTitle, offeredDate, processInstanceId);	
	*/
	return self;
}

- (NSString *) offeredDateString {
    NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -24*60*60];
    if ([offeredDate compare:yesterday] == NSOrderedAscending) {
        [dateFormatter setDateFormat:@"yyyy/MM/dd"];        
    } else {
        [dateFormatter setDateFormat:@"H:mm"];
    }
    return [dateFormatter stringFromDate:offeredDate];
}

@end
